﻿using System;
using BoldBrick.Core.Logging;
using BoldBrick.OneIntranet.HR.DataObjects;
using BoldBrick.OneIntranet.HR.DataObjects.Lists;
using BoldBrick.OneIntranet.HR.Resources;
using BoldBrick.OneIntranet.HR.Workflows;
using BoldBrick.SharePoint.CommonControls.Layouts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using BoldBrick.SharePoint.CommonControls.Extensions;

namespace BoldBrick.OneIntranet.HR.Layouts
{
	public partial class StartHRWorkflowPage : BaseDialogPage
	{
		private static readonly ILogger log = Logger.GetLogger(typeof(OpenPositionPage));

		#region ------ BaseDialogPage Members -----------------------------------------------------

		protected override string GetDescription(uint language)
		{
			this.DialogIconType = DialogIconTypes.Question;

			SPListItem itemCandidate = GetListItem(language);
			if (itemCandidate == null)
				return null;

			
			// check if workflow is runing
			SPWorkflowState workflowState = Lists.CandidatesList.HrWorkflow.GetWorkflowState(SPContext.Current.Site, itemCandidate);
			workflowState = workflowState & SPWorkflowState.Running;
			if (workflowState == SPWorkflowState.Running)
			{
				string message = ResourceBuilder.GetResource("StartHRWorkflowPage_Error_WorkflowIsRunning", language);
				log.Write(LogLevel.Info, "Workflow is running. Abort.");
				ShowError(message, true);
				return null;
			}

			// get workflow item
			SPListItem itemWorkflow;
			try
			{
				itemWorkflow = HRWorkflowHelper.GetWorkflowItemByCandidateItem(SPContext.Current.Web, itemCandidate);
			}
			catch (HRWorkflowException ex)
			{
				log.Write(LogLevel.Warn, ex, "'GetWorkflowItemByCandidateItem' failed.");
				ShowError(ex.Message, true);
				return null;
			}

			// check workflow
			string errorMessage;
			if (!HRWorkflowHelper.CheckWorkflow(SPContext.Current.Web, itemWorkflow, out errorMessage))
			{
				log.Write(LogLevel.Warn, "'CheckWorkflow' failed: {0}", errorMessage);
				ShowError(errorMessage, true);
				return null;
			}

			// build name of candidate
			string candidateName = itemCandidate.GetValueObject<string>(SiteColumns.LastNameColumn.InternalName);
			string candidateFirstName = itemCandidate.GetValueObject<string>(SiteColumns.FirstNameColumn.InternalName);
			if (!string.IsNullOrEmpty(candidateFirstName))
			{
				candidateName = string.Format("{0} {1}", candidateName, candidateFirstName);
			}

			// build description
			string description = ResourceBuilder.GetResource("StartHRWorkflowPage_Description", language, 
				itemWorkflow.Title, candidateName);

			return description;
		}

		protected override string GetCancelButtonText(uint language)
		{
			return ResourceBuilder.GetResource("StartHRWorkflowPage_CancelButton_Text", language);
		}

		protected override string GetOKButtonText(uint language)
		{
			return ResourceBuilder.GetResource("StartHRWorkflowPage_OkButton_Text", language);
		}

		protected override string GetTitle(uint language)
		{
			return ResourceBuilder.GetResource("StartHRWorkflowPage_Title", language);
		}

		#endregion

		#region ------ Action Methods -------------------------------------------------------------

		protected override bool DoAction(out string errorMessage)
		{
			errorMessage = null;

			uint language = SPContext.Current.Web.Language;
			SPListItem itemCandidate = GetListItem(language);
			if (itemCandidate == null)
				return false;

			string workflowName = Lists.CandidatesList.HrWorkflow.GetName(language);

			
			SPWorkflowAssociation workflowAssociation = itemCandidate.ContentType.WorkflowAssociations.GetAssociationByName(workflowName, SPContext.Current.Web.Locale);
			SPContext.Current.Site.WorkflowManager.StartWorkflow(itemCandidate, workflowAssociation, workflowAssociation.AssociationData);

			return true;
		}

		#endregion

		#region ------ Events ---------------------------------------------------------------------

		protected void buttonStartWorkflow_Click(object sender, EventArgs e)
		{
			
		}

		#endregion
	}
}
